package com.project.weimi.activity;

import com.project.weimi.R;
import com.project.weimi.application.Constant;
import com.project.weimi.utils.SetClickListener;
import com.project.weimi.utils.SharedPreferencesHelper;
import com.project.weimi.widget.ClearCachePopupWindow;
import com.project.weimi.widget.ToastMessage;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.ToggleButton;

/**
 * 通用设置Activity
 * @author Administrator
 *
 */
public class CommonSettingActivity extends BaseActivity implements OnCheckedChangeListener{

	private RelativeLayout rl_newMsg_set,rl_clear_cache,rl_grade,rl_exitCurrentAccount;
	private ToggleButton btn_toggle;
	
	private ClearCachePopupWindow popupWindow;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentViewWithBar(R.layout.activity_common_setting, "通用设置");
		
		initView();
		
		
	}
	
	private void initView(){
		rl_newMsg_set = (RelativeLayout)findViewById(R.id.rl_newMsg_set);
		rl_clear_cache = (RelativeLayout)findViewById(R.id.rl_clear_cache);
		rl_grade = (RelativeLayout)findViewById(R.id.rl_grade);
		rl_exitCurrentAccount = (RelativeLayout)findViewById(R.id.rl_exitCurrentAccount);
		btn_toggle = (ToggleButton)findViewById(R.id.btn_toggle);
		
		// 监听
		btn_toggle.setOnCheckedChangeListener(this);
		SetClickListener.setClickListener(listener, rl_newMsg_set,rl_clear_cache,rl_grade,rl_exitCurrentAccount);
	}
	
	private OnClickListener listener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			Intent intent = null;
			
			switch (v.getId()) {
			case R.id.rl_newMsg_set: //新消息设置
				intent = new Intent(CommonSettingActivity.this,NewMsgSettingActivity.class);
				startActivity(intent);
				break;
			case R.id.rl_clear_cache: //清除缓存
				popupWindow = new ClearCachePopupWindow(CommonSettingActivity.this, itemsOnClick);
				// 设置layout在PopupWindow中显示的位置
				popupWindow.showAtLocation(CommonSettingActivity.this.findViewById(R.id.ll_clear_cache), 
						Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
				break;
			case R.id.rl_grade: //给应用评分
				
				break;
			case R.id.rl_exitCurrentAccount: //退出当前账号
				if(rl_exitCurrentAccount.getVisibility() == View.VISIBLE){
					SharedPreferencesHelper spHelper = new SharedPreferencesHelper(
							CommonSettingActivity.this,Constant.USER_INFO);
					spHelper.clear();
					rl_exitCurrentAccount.setVisibility(View.GONE);
					
					intent = new Intent(CommonSettingActivity.this,MainActivity.class);
					intent.putExtra("fragId", 0);
					startActivity(intent);
				} 
				break;
			}
		}
	};

	private OnClickListener itemsOnClick = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			switch (v.getId()) {
			case R.id.btn_clear: // 确定清空
				popupWindow.dismiss();
				break;
			case R.id.btn_cancel: // 取消
				popupWindow.dismiss();
				break;
			}
		}
	};
	
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if(isChecked){
			ToastMessage.showMyToast(CommonSettingActivity.this, "已开启");
		}else{
			ToastMessage.showMyToast(CommonSettingActivity.this, "已关闭");
		}
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		if(null != memberId && !TextUtils.isEmpty(memberId)){
			//Log.e("TAG", "onStart----CommonSettingActivity有memberId===---"+memberId);
			rl_exitCurrentAccount.setVisibility(View.VISIBLE);
		} else {
			//Log.e("TAG", "onStart----CommonSettingActivity无memberId===---"+memberId);
			rl_exitCurrentAccount.setVisibility(View.GONE);
		}
	}
	
	@Override
	void setStatusBar(int statusBarColorRes) {
		super.setStatusBar(R.color.transparent_3);
	}
	
	
}
